﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eassets.Service.Global;
using Eassets.Layered.Domain.Global;
using System.Collections.ObjectModel;

namespace Eassets.UI.GlobalReference
{
    public partial class OrganizationLevelListForm : System.Web.UI.Page
    {
        private Boolean checkSaveModify;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadOrganizationLevels();

                LoadOrganizationLevelList(Convert.ToInt64(dropDownListOrganizationLevel.SelectedItem.Value));

                textBoxOrganizationLevelCode.Attributes.Add("onChange", "javascript:this.value=toUpperCase(this.value);");
                textBoxOrganizationLevelCode.Attributes.Add("onKeyPress", "javascript:return validateCodeForSymbols(event)");
                textBoxDescription.Attributes.Add("onChange", "javascript:this.value=toTitleCase(this.value)");

                ViewState["ButtonBehaviour"] = "Load";
                DisableComponants();
            }
        }

        protected void buttonCancel_Click(object sender, EventArgs e)
        {
            ClearInputs();
            textBoxOrganizationLevelCode.Focus();
        }

        protected void buttonCancelModify_Click(object sender, EventArgs e)
        {
            ClearInputs();
            //accordionInputs.SelectedIndex = 0;
            textBoxOrganizationLevelCode.Focus();
        }

        protected void buttonDelete_Click(object sender, EventArgs e)
        {
            if (hiddenFieldOrganizationLevel.Value == String.Empty)
            {
                FlashMessage1.Display("No record is selected", "errormessage");
            }
            else
            {
                OrganizationLevelListService businessUnitlistservice = new OrganizationLevelListService();

                try
                {
                    if (Page.IsValid)
                    {
                        businessUnitlistservice.DeleteOrganizationLevelList(Convert.ToInt64(hiddenFieldOrganizationLevel.Value));
                        FlashMessage1.Message = "Successfully Deleted";
                        FlashMessage1.CssClass = "okmessage";
                        FlashMessage1.Display();
                    }
                }
                catch (Exception exception)
                {
                    FlashMessage1.Message = "Deleting Failed";
                    FlashMessage1.CssClass = "errormessage";
                    FlashMessage1.Display();
                }

                LoadOrganizationLevelList(Convert.ToInt64(dropDownListOrganizationLevel.SelectedItem.Value));
                ClearInputs();
            }
        }

        //protected void ButtonSave_Click(object sender, EventArgs e)
        //{
        //    if (checkSaveModify == false)
        //    {
        //        if (CheckMandatory("A"))
        //        {
        //            //OrganizationLevelList organizationLevelList = new OrganizationLevelList();
        //            //OrganizationLevelListService organizationLevelListService = new OrganizationLevelListService();

        //            var obj = new OrganizationLevelList()
        //            {
        //                OrganizationLevelId = Convert.ToInt64(dropDownListOrganizationLevel.SelectedValue),
        //                Code = textBoxOrganizationLevelCode.Text.Trim().ToUpper(),
        //                Description = textBoxDescription.Text.Trim(),
        //                CompanyStakeholderId =
        //                    Convert.ToInt64(Session[Global.KeyCurrentCompanyStakeholderId])
        //            };
        //            //organizationLevelList.OrganizationLevelId =
        //            //    Convert.ToInt64(dropDownListOrganizationLevel.SelectedValue);
        //            //organizationLevelList.Code = textBoxOrganizationLevelCode.Text.Trim();
        //            //organizationLevelList.Description = textBoxDescription.Text.Trim();
        //            //organizationLevelList.CompanyStakeholderId =
        //            //    Convert.ToInt64(Session[Global.KeyCurrentCompanyStakeholderId]);

        //            try
        //            {
        //                if (Page.IsValid)
        //                {
        //                    //organizationLevelListService.AddOrganizationLevelList(organizationLevelList);
        //                    new OrganizationLevelListService().AddOrganizationLevelList(obj);
        //                    FlashMessage1.Message = "Successfully Saved";
        //                    FlashMessage1.CssClass = "okmessage";
        //                    FlashMessage1.Display();
        //                    ClearInputs();
        //                    textBoxOrganizationLevelCode.Focus();
        //                }
        //            }
        //            catch (Exception exception)
        //            {
        //                FlashMessage1.Message = "Record Already Exists";
        //                FlashMessage1.CssClass = "errormessage";
        //                FlashMessage1.Display();
        //            }

        //            LoadOrganizationLevelList(Convert.ToInt64(dropDownListOrganizationLevel.SelectedItem.Value));
        //            // clearInputs();
        //        }
        //    }
        //    else
        //    {
        //        if (hiddenFieldOrganizationLevel.Value != String.Empty)
        //        {
        //            if (CheckMandatory("M"))
        //            {
        //                OrganizationLevelList organizationLevelList = new OrganizationLevelList();
        //                OrganizationLevelListService organizationLevelListService = new OrganizationLevelListService();
        //                organizationLevelList.OrganizationLevelListId = Convert.ToInt64(hiddenFieldOrganizationLevel.Value);
        //                organizationLevelList.Code = textBoxOrganizationLevelCode.Text.Trim().ToString();
        //                organizationLevelList.Description = textBoxDescription.Text.Trim().ToString();
        //                organizationLevelList.OrganizationLevelId = Convert.ToInt64(dropDownListOrganizationLevel.SelectedItem.Value);

        //                try
        //                {
        //                    if (Page.IsValid)
        //                    {
        //                        organizationLevelListService.ModifyOrganizationLevelList(organizationLevelList);
        //                        FlashMessage1.Message = "Successfully Saved";
        //                        FlashMessage1.CssClass = "okmessage";
        //                        FlashMessage1.Display();
        //                    }
        //                }
        //                catch (Exception exception)
        //                {
        //                    FlashMessage1.Message = "Saving Failed";
        //                    FlashMessage1.CssClass = "errormessage";
        //                    FlashMessage1.Display();
        //                }

        //                LoadOrganizationLevelList(Convert.ToInt64(dropDownListOrganizationLevel.SelectedItem.Value));
        //                ClearInputs();
        //            }
        //        }
        //        else
        //        {
        //            FlashMessage1.Message = "Select a record to Modify";
        //            FlashMessage1.CssClass = "errormessage";
        //            FlashMessage1.Display();
        //        }
        //    }
        //}

        protected void buttonSaveModify_Click(object sender, EventArgs e)
        {
            checkSaveModify = true;
        }

        protected void dropDownListOrganizationLevel_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadOrganizationLevelList(Convert.ToInt64(dropDownListOrganizationLevel.SelectedItem.Value));
            ClearInputs();
            //accordionInputs.SelectedIndex = 0;
            //accordionPaneAdd.Visible = true;
        }

        protected void gridViewOrganizationLevelList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells.Count > 1)
            {
                e.Row.Cells[1].Visible = false;
                e.Row.Cells[4].Visible = false;
            }
        }

        protected void gridViewOrganizationLevelList_SelectedIndexChanged(object sender, EventArgs e)
        {
            //OpenArc.Erims.Domain.Ecorp.OrganizationLevelList OrganizationLevelList = new OpenArc.Erims.Domain.Ecorp.OrganizationLevelList();
            //OrganizationLevelListService OrganizationLevelListService = new OrganizationLevelListService();
            GridViewRow row = gridViewOrganizationLevelList.SelectedRow;
            hiddenFieldOrganizationLevel.Value = row.Cells[1].Text.Trim();
            textBoxOrganizationLevelCode.Text = row.Cells[2].Text.Trim();
            textBoxDescription.Text = row.Cells[3].Text.Trim();

            ViewState["ButtonBehaviour"] = "GridSelectedChanged";
            DisableComponants();
        }

        protected void gridViewOrganizationLevelList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (dropDownListOrganizationLevel.SelectedIndex > -1)
            {
                LoadOrganizationLevelList(Convert.ToInt64(dropDownListOrganizationLevel.SelectedValue));
                gridViewOrganizationLevelList.PageIndex = e.NewPageIndex;
                gridViewOrganizationLevelList.DataBind();
            }
        }

        private void LoadOrganizationLevels()
        {
            OrganizationLevelService organizationLevelService = new OrganizationLevelService();
            Collection<OrganizationLevel> organizationLevels = new Collection<OrganizationLevel>();

            try
            {
                organizationLevels = organizationLevelService.GetAllOrganizationLevel();
            }
            catch (Exception exception)
            {}
            
            dropDownListOrganizationLevel.DataSource = organizationLevels;
            dropDownListOrganizationLevel.DataValueField = "OrganizationLevelId";
            dropDownListOrganizationLevel.DataTextField = "Description";
            dropDownListOrganizationLevel.DataBind();
        }

        private Boolean CheckMandatory(string addOrModify)
        {
            if (addOrModify == "A")
            {
                if (textBoxOrganizationLevelCode.Text.Trim() == String.Empty)
                {
                    FlashMessage1.Display("[Business Unit List Code] cannot be blank", "errormessage");
                    return false;
                }
                else if (textBoxDescription.Text.Trim() == String.Empty)
                {
                    FlashMessage1.Display("[Description] cannot be blank", "errormessage");
                    return false;
                }
            }

            return true;
        }

        private void LoadOrganizationLevelList(long OrganizationLevelID)
        {
            OrganizationLevelListService organizationLevelListService = new OrganizationLevelListService();
            List<OrganizationLevelList> organizationLevelLists = new List<OrganizationLevelList>();
            //gridViewOrganizationLevelList.DataSource = businessunitlistservice.GetAllOrganizationLevelList(Convert.ToInt64(dropDownListOrganizationLevel.SelectedItem.Value));
            organizationLevelLists = organizationLevelListService.GetAllOrganizationLevelList().Where(x => x.OrganizationLevelId != null && x.OrganizationLevelId == OrganizationLevelID).ToList();
            gridViewOrganizationLevelList.DataSource = organizationLevelLists;
            gridViewOrganizationLevelList.DataBind();
        }

        private void ClearInputs()
        {
            textBoxOrganizationLevelCode.Text = "";
            textBoxDescription.Text = "";
            hiddenFieldOrganizationLevel.Value = "";
            //textBoxOrganizationLevelCodeModify.Text = "";
            //textBoxDescriptionModify.Text = "";
        }


        protected void gridViewContactDetails_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells.Count > 1)
            {
                e.Row.Cells[1].Visible = false;
            }
        }


        //private void ClearContactInputs()
        //{
        //    try
        //    {
        //        textBoxContactInfo.Text = "";
        //        dropDownListCountry.SelectedIndex = -1;
        //        dropDownListCountryLevel2.SelectedIndex = -1;
        //        dropDownListCountryLevel3.SelectedIndex = -1;
        //        dropDownListCountryLevel4.SelectedIndex = -1;
        //        dropDownListCountryLevel5.SelectedIndex = -1;
        //        dropDownListCountryLevel6.SelectedIndex = -1;
        //        textBoxOtherDetails.Text = "";
        //    }
        //    catch (Exception exception)
        //    {
        //    }
        //}



        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            ViewState["ButtonBehaviour"] = "Add";
            DisableComponants();
        }

        protected void ButtonModify_Click(object sender, EventArgs e)
        {
            //if (hiddenFieldOrganizationLevel.Value != String.Empty)
            //{
            //    if (CheckMandatory("M"))
            //    {
            //        OrganizationLevelList organizationLevelList = new OrganizationLevelList();
            //        OrganizationLevelListService organizationLevelListService = new OrganizationLevelListService();
            //        //organizationLevelList.OrganizationLevelListId = Convert.ToInt64(hiddenFieldOrganizationLevel.Value);
            //        //organizationLevelList.Code = textBoxOrganizationLevelCodeModify.Text.Trim().ToString();
            //        //organizationLevelList.Description = textBoxDescriptionModify.Text.Trim().ToString();
            //        //organizationLevelList.OrganizationLevelId = Convert.ToInt64(dropDownListOrganizationLevel.SelectedItem.Value);
            //        //organizationLevelList.OrganizationLevelMode = radioButtonListBuTypeModify.SelectedValue;
            //        try
            //        {
            //            if (Page.IsValid)
            //            {
            //                organizationLevelListService.ModifyOrganizationLevelList(organizationLevelList);
            //                FlashMessage1.Message = "Successfully Saved";
            //                FlashMessage1.CssClass = "okmessage";
            //                FlashMessage1.Display();
            //            }
            //        }
            //        catch (Exception exception)
            //        {
            //            FlashMessage1.Message = "Saving Failed";
            //            FlashMessage1.CssClass = "errormessage";
            //            FlashMessage1.Display();
            //        }

            //        LoadOrganizationLevelList(Convert.ToInt64(dropDownListOrganizationLevel.SelectedItem.Value));
            //ClearInputs();
            //    }
            //}
            //else
            //{
            //    FlashMessage1.Message = "Select a record to Modify";
            //    FlashMessage1.CssClass = "errormessage";
            //    FlashMessage1.Display();
            //}
            checkSaveModify = false;
            ViewState["ButtonBehaviour"] = "Modify";
            DisableComponants();
        }

        protected void ButtonDelete_Click(object sender, EventArgs e)
        {
            if (hiddenFieldOrganizationLevel.Value == String.Empty)
            {
                FlashMessage1.Display("No record is selected", "errormessage");
            }
            else
            {
                OrganizationLevelListService businessUnitlistservice = new OrganizationLevelListService();

                try
                {
                    if (Page.IsValid)
                    {
                        businessUnitlistservice.DeleteOrganizationLevelList(Convert.ToInt64(hiddenFieldOrganizationLevel.Value));
                        FlashMessage1.Message = "Successfully Deleted";
                        FlashMessage1.CssClass = "okmessage";
                        FlashMessage1.Display();
                    }
                }
                catch (Exception exception)
                {
                    FlashMessage1.Message = "Deleting Failed";
                    FlashMessage1.CssClass = "errormessage";
                    FlashMessage1.Display();
                }

                LoadOrganizationLevelList(Convert.ToInt64(dropDownListOrganizationLevel.SelectedItem.Value));
                ClearInputs();
            }
        }

        protected void buttonSave_Click(object sender, EventArgs e)
        {
            if (CheckMandatory("A"))
            {
                //OrganizationLevelList organizationLevelList = new OrganizationLevelList();
                //OrganizationLevelListService organizationLevelListService = new OrganizationLevelListService();

                var obj = new OrganizationLevelList()
                {
                    OrganizationLevelId = Convert.ToInt64(dropDownListOrganizationLevel.SelectedValue),
                    Code = textBoxOrganizationLevelCode.Text.Trim().ToUpper(),
                    Description = textBoxDescription.Text.Trim(),
                    CompanyStakeholderId = Convert.ToInt64(Session[Global.KeyCurrentCompanyStakeholderId])
                };
                //organizationLevelList.OrganizationLevelId
                //    = Convert.ToInt64(dropDownListOrganizationLevel.SelectedValue);
                //organizationLevelList.Code = textBoxOrganizationLevelCode.Text.Trim().ToUpper();
                //organizationLevelList.Description = textBoxDescription.Text.Trim();
                //organizationLevelList.CompanyStakeholderId =

                //        Convert.ToInt64(
                //        Session[Global.KeyCurrentCompanyStakeholderId]);

                //organizationLevelList.sys_DateCreation = DateTime.Now;
                //organizationLevelList.sys_DateLastModification = DateTime.Now;

                try
                {
                    if (Page.IsValid)
                    {
                        //organizationLevelListService.AddOrganizationLevelList(organizationLevelList);
                        new OrganizationLevelListService().AddOrganizationLevelList(obj);
                        FlashMessage1.Message = "Successfully Saved";
                        FlashMessage1.CssClass = "okmessage";
                        FlashMessage1.Display();
                        ClearInputs();
                        textBoxOrganizationLevelCode.Focus();
                    }
                }
                catch (Exception exception)
                {
                    FlashMessage1.Message = "Record Already Exists";
                    FlashMessage1.CssClass = "errormessage";
                    FlashMessage1.Display();
                }

                LoadOrganizationLevelList(Convert.ToInt64(dropDownListOrganizationLevel.SelectedItem.Value));
                ViewState["ButtonBehaviour"] = "Save";
                DisableComponants();
            }
        }

        protected void ButtonClear_Click(object sender, EventArgs e)
        {

        }

        protected void DisableComponants()
        {
            if (ViewState["ButtonBehaviour"] == "Add")
            {
                buttonModify.Enabled = false;

                buttonDelete.Enabled = false;

                textBoxOrganizationLevelCode.Enabled = true;
                textBoxOrganizationLevelCode.Text = String.Empty;
                textBoxDescription.Enabled = true;
                textBoxDescription.Text = string.Empty;
            }
            else if (ViewState["ButtonBehaviour"] == "Modify")
            {
                buttonAdd.Enabled = false;
                buttonDelete.Enabled = false;
                textBoxOrganizationLevelCode.Enabled = true;
                textBoxDescription.Enabled = true;

            }
            else if (ViewState["ButtonBehaviour"] == "Delete")
            {

            }
            else if (ViewState["ButtonBehaviour"] == "Save")
            {

            }
            else if (ViewState["ButtonBehaviour"] == "Clear")
            {

            }

            else if (ViewState["ButtonBehaviour"] == "Load")
            {
                textBoxOrganizationLevelCode.Enabled = false;
                textBoxOrganizationLevelCode.Text = string.Empty;
                textBoxDescription.Enabled = false;
                textBoxDescription.Text = string.Empty;
                buttonAdd.Enabled = true;

                buttonModify.Enabled = false;
                buttonDelete.Enabled = false;
                buttonDelete.Enabled = false;

            }
            else if (ViewState["ButtonBehaviour"] == "GridSelectedChanged")
            {
                buttonAdd.Enabled = false;
                buttonModify.Enabled = true;
                buttonDelete.Enabled = true;
            }

        }
    }
}